package com.zr.controller;

import com.zr.controller.R.Code;
import com.zr.controller.R.Result;
import com.zr.pojo.ArticleCate;
import com.zr.service.ArticleCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/my/artcate")
@CrossOrigin
public class ArticleCateController {

    @Autowired
    ArticleCateService articleCateService;

//    新增文章分类
    @PostMapping("/addcates")
    public Result addCate(ArticleCate articleCate){
        System.out.println("in add...");
//        System.out.println(articleCate);
        Boolean flag = articleCateService.add(articleCate);
        return new Result(flag? Code.SAVE_OK:Code.SAVE_ERR, null, flag?"添加文章分类成功！":"添加文章分类失败，或存在同名分类。");
    }

//    查询所有文章分类
    @GetMapping("/cates")
    public Result selectAllCate(){
//        System.out.println("in selece all cate...");
        List<ArticleCate> articleCates = articleCateService.selectAllCate();
        boolean flag = articleCates != null;
        return new Result(flag?Code.GET_OK:Code.GET_ERR, articleCates, flag?"查询所有文章分类成功！":"暂无文章分类或查询失败！");
    }

//    根据id删除文章分类（逻辑删除）
    @GetMapping("/deletecate/{id}")
    public Result deleteById(@PathVariable int id){
//        System.out.println("in delete...");
//        System.out.println(id);
        Boolean flag = articleCateService.deleteById(id);
        return new Result(flag?Code.DELETE_OK:Code.DELETE_ERR, null, flag?"删除文章分类成功！":"删除文章分类失败！");
    }

//    根据id修改文章分类
    @PostMapping("/updatecate")
    public Result updateById(ArticleCate articleCate){
        System.out.println("in update...");
        Boolean flag = articleCateService.updateById(articleCate);
        return new Result(flag?Code.UPDATE_OK:Code.UPDATE_ERR, null, flag?"修改文章分类成功！":"修改文章分类失败，或存在同名分类。");
    }
// 根据id查询分类
    @GetMapping("/cates/{id}")
    public Result selectById(@PathVariable int id){
        ArticleCate articleCate = articleCateService.selectById(id);
        boolean flag = articleCate != null;
        return new Result(flag?Code.GET_OK:Code.GET_ERR, articleCate, flag?"查询文章分类成功！":"暂无该文章分类或查询失败！");
    }
}
